package org.groupg.project.configmanager.ui;


import com.formdev.flatlaf.FlatIntelliJLaf;
import org.groupg.project.configmanager.util.JPAUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;

public class ConfigManagerApp {
    private static final Logger logger = LoggerFactory.getLogger(ConfigManagerApp.class);

    public static void main(String[] args) {
        // 设置未捕获异常处理器
        Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {
            logger.error("未捕获的异常", throwable);
            JOptionPane.showMessageDialog(null,
                    "应用程序发生错误: " + throwable.getMessage(),
                    "错误",
                    JOptionPane.ERROR_MESSAGE);
        });

        // 添加关闭钩子，确保JPA正常关闭
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            logger.info("应用程序正在关闭...");
            JPAUtil.close();
        }));

        // 设置系统外观
        try {
            UIManager.setLookAndFeel(new FlatIntelliJLaf());
        } catch (Exception e) {
            logger.warn("设置系统外观失败", e);
        }

        // 启动主界面
        SwingUtilities.invokeLater(() -> {
            try {
                MainFrame mainFrame = new MainFrame();
                mainFrame.setVisible(true);
                logger.info("配置管理工具启动成功");
            } catch (Exception e) {
                logger.error("启动主界面失败", e);
                JOptionPane.showMessageDialog(null,
                        "启动主界面失败: " + e.getMessage(),
                        "错误",
                        JOptionPane.ERROR_MESSAGE);
            }
        });
    }
}